package com.ydgk.controller;

import com.ydgk.bean.*;
import com.ydgk.service.*;
import com.ydgk.vo.HomeInfo;
import com.ydgk.vo.OrderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class DispatcherControllerForManager {

    @Autowired
    private KindsService kindsService;

    @Autowired
    private KindsTypeService kindsTypeService;

    @Autowired
    private FurnitureService furnitureService;

    @Autowired
    private FurnitureKindsService furnitureKindsService;

    @Autowired
    private ViewService viewService;

    @Autowired
    private DetailService detailService;

    @Autowired
    private OrderService orderService;

    @Autowired
    private MyOrdersService myOrdersService;

    @Autowired
    private HomeService homeService;

    @ModelAttribute
    public void getOrderInfo(@RequestParam(value = "oid" , required = false , defaultValue = "0") Integer oid,Map map){
        if(oid != 0){
            Order order = orderService.findOrderById(oid);
            OrderInfo orderInfo = orderService.getOrderInfo(order);
            map.put("orderInfo",orderInfo);
        }
    }

    @ModelAttribute
    public void getAttribute(@RequestParam(value = "kid" , required = false , defaultValue = "0") Integer kid,
                              @RequestParam(value = "ktid" , required = false , defaultValue = "0") Integer ktid,
                              @RequestParam(value = "fid" , required = false , defaultValue = "0") Integer fid,
                              @RequestParam(value = "pageNo" , required = false , defaultValue = "1") Integer pageNo,
                              @RequestParam(value = "target" , required = false , defaultValue = "allFurniture") String target,
                              @RequestParam(value = "orderTarget" , required = false , defaultValue = "allOrder") String orderTarget,
                              Map map){

        if(kid != 0){
            Kinds kinds = kindsService.findById(kid);
            map.put("kinds",kinds);
        }else{
            map.put("kinds",null);
        }

        if(ktid != 0){
            KindsType kindsType = kindsTypeService.findById(ktid);
            map.put("kindsType",kindsType);
        }else{
            map.put("kindsType",null);
        }

        if(fid != 0){
            Furniture furniture = furnitureService.findById(fid);
            List<View> viewList = viewService.findByFid(fid);
            List<Detail> detailList = detailService.findByFid(fid);
            List<FurnitureKinds> furnitureKindsList = furnitureKindsService.findByFid(fid);
            map.put("furniture",furniture);
            map.put("viewList",viewList);
            map.put("detailList",detailList);
            map.put("furnitureKindsList",furnitureKindsList);
        }else{
            map.put("furniture",null);
        }

        map.put("pageNo",pageNo);
        map.put("target",target);
        map.put("orderTarget",orderTarget);
    }

@RequestMapping("/toKindsType")
    public String toKindsType(Kinds kinds){
        if(kinds != null){
            return "Manager/kindsType";
        }else{
            return "redirect:/toError";
        }
}

    @RequestMapping("/toFurniture")
    public String toFurniture(KindsType kindsType, Kinds kinds){
        if(kindsType != null && kinds != null){
            return "Manager/furniture";
        }else{
            return "redirect:/toError";
        }
    }

    @RequestMapping("/toFurniture_Insert")
    public String toFurniture_Insert(KindsType kindsType, Kinds kinds){
        if(kindsType != null && kinds != null){
            FurnitureController.clear();
            return "Manager/furniture_insert";
        }else{
            return "redirect:/toError";
        }
    }

    @RequestMapping("/toFurniture_Detail")
    public String toFurnitureDetail(KindsType kindsType, Kinds kinds, Furniture furniture){
        if(kindsType != null && kinds != null && furniture != null){
            FurnitureController.clear();
            ViewController.clear();
            DetailController.clear();
            FurnitureKindsController.clear();
            return "Manager/furniture_detail";
        }else{
            return "redirect:/toError";
        }
    }

    @RequestMapping("/toMyOrders")
    public String toOrder_Detail(){
            return "Manager/myOrders";
    }

    @RequestMapping("/toOrder_Detail")
    public String toOrder_Detail(OrderInfo orderInfo, Map map){
        if(orderInfo != null){
            return "Manager/order_detail";
        }else{
            return "redirect:/toError";
        }
    }

    @RequestMapping("/toMyLogistics")
    public String toMyLogistics(Map map){
        List<Order> orderList = myOrdersService.findAllSendOrder();
        List<OrderInfo> orderInfoList = new ArrayList<>();
        for (Order o : orderList) {
            OrderInfo orderInfo = orderService.getOrderInfo(o);
            orderInfoList.add(orderInfo);
        }
        map.put("orderInfoList",orderInfoList);
        return "Manager/myLogistics";
    }

    @RequestMapping("/toLogistics_Detail")
    public String toLogistics_Detail(OrderInfo orderInfo, Map map){
        if(orderInfo != null){
            return "Manager/logistics_detail";
        }else{
            return "redirect:/toError";
        }
    }

    @RequestMapping("/toManager")
    public String toManager(Map map){
        HomeInfo homeInfo = homeService.getHomeInfo();
        map.put("homeInfo",homeInfo);
        return "Manager/main";
    }
}
